package edu.spbsu.nonsm00th.model;

import edu.spbsu.nonsm00th.util.Precondition;

/**
 * Line representation class.
 *
 * @author mukhadin
 */
public class Line {

    private Point a;
    private Point b;
    private int dimension;

    public Line(Point a, Point b) {
        Precondition.argsNotNull(a, b);
        Precondition.check(a.getDimension() == b.getDimension(), "Dimensions of points should be equal.");
        Precondition.check(!a.equals(b), "Points a and b can not be equal");

        this.a = a;
        this.b = b;
        this.dimension = a.getDimension();
    }

    /**
     * Calculates the distance between line <code>l</code> and point
     * <code>p</code>.
     * Should be used only for 3-dimension case.
     *
     * @param l the line
     * @param p the point
     * @return the distance
     */
    public double getDistance(Point p) {
        Precondition.argsNotNull(p);
        Precondition.check(dimension == 3, "Dimension of points should be equal to 3");
        Precondition.check(p.getDimension() == 3, "Dimension of points should be equal to 3");

        Vector ab = new Vector(a, b);
        Vector ap = new Vector(a, p);
        double area = Vector.crossProduct(ap, ab).getNorm();
        return area/ab.getNorm();
    }

    public Point getA() {
        return a;
    }

    public void setA(Point a) {
        this.a = a;
    }

    public Point getB() {
        return b;
    }

    public void setB(Point b) {
        this.b = b;
    }

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

}
